﻿using Newtonsoft.Json;
using ReactiveUI.Fody.Helpers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Common.ViewModels
{

    /// <summary>
    /// 前进线向量
    /// </summary>
    [Serializable]
    [JsonObject(MemberSerialization.OptOut)]
    public class TransformRatio : ViewModelBase
    {
        public static readonly TransformRatio X = new TransformRatio() { RatioX = 1, RatioY = 0, RatioZ = 0 };

        public static readonly TransformRatio Y = new TransformRatio() { RatioX = 0, RatioY = 1, RatioZ = 0 };

        public static readonly TransformRatio Z = new TransformRatio() { RatioX = 0, RatioY = 0, RatioZ = 1 };

        /// <summary>
        /// X系数
        /// </summary>
        [Reactive] public double RatioX { get; set; }

        /// <summary>
        /// Y系数
        /// </summary>
        [Reactive] public double RatioY { get; set; }

        /// <summary>
        /// Z系数
        /// </summary>
        [Reactive] public double RatioZ { get; set; }


        /// <summary>
        /// 计算两个3D向量的叉乘（叉乘的结果仍是向量）
        /// </summary>
        /// <param name="vector1"></param>
        /// <param name="vector2"></param>
        /// <returns></returns>
        public static TransformRatio CrossProduct(TransformRatio vector1, TransformRatio vector2)
        {
            TransformRatio transformRatio = new TransformRatio(
                vector1.RatioY * vector2.RatioZ - vector1.RatioZ * vector2.RatioY,
                vector1.RatioZ * vector2.RatioX - vector1.RatioX * vector2.RatioZ,
                vector1.RatioX * vector2.RatioY - vector1.RatioY * vector2.RatioX
            );
            transformRatio.Normalize();
            return transformRatio;
        }

        public TransformRatio()
        {

        }

        public TransformRatio(double X, double Y, double Z)
        {
            this.RatioX = X; this.RatioY = Y; this.RatioZ = Z;
        }

        // 单位化向量
        public void Normalize()
        {
            double length = Math.Sqrt(RatioX * RatioX + RatioY * RatioY + RatioZ * RatioZ);
            RatioX /= length;
            RatioY /= length;
            RatioZ /= length;
        }

        public void Update(TransformRatio transform)
        {
            this.RatioX = transform.RatioX;
            this.RatioY = transform.RatioY;
            this.RatioZ = transform.RatioZ;
        }
    }
}
